package com.showde.shoppingmall.activity;

import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.kim.androidbase.tools.StringUtils;
import com.showde.shoppingmall.R;
import com.showde.shoppingmall.business.logic.AddressLogic;
import com.showde.shoppingmall.business.logic.CallbackData;
import com.showde.shoppingmall.business.logic.UICallbackInterface;
import com.showde.shoppingmall.domain.Address;
import com.showde.shoppingmall.utils.ActivityDispatch;
import com.showde.shoppingmall.utils.NetManager;
import com.showde.shoppingmall.utils.ToastUtils;

public class AddressEditActivity extends BaseActivity {

	/**
	 * 添加地址成功
	 */
	private static final int MSG_ADD_ADDRESS_SUCCESS = 0x10001;
	/**
	 * 添加地址失败
	 */
	private static final int MSG_ADD_ADDRESS_FAIL = 0x10002;

	private EditText addressName; // 收件人姓名
	private EditText addressProvince;// 收件人省份
	private EditText addressCity; // 收件人城市
	private EditText addressArea; // 收件人地区
	private EditText addressStreet; // 收件人街道
	private EditText addressPostcode;// 收件人邮编
	private EditText addressPhone; // 收件人手机
	private EditText addressTel;// 收件人电话

	Address address;

	private TextView complete;

	Handler handler = new Handler() {

		@Override
		public void handleMessage(Message msg) {
			switch (msg.what) {
			case MSG_ADD_ADDRESS_FAIL:
				ToastUtils.showToast(AddressEditActivity.this, "添加地址失败");
				break;
			case MSG_ADD_ADDRESS_SUCCESS:
				ToastUtils.showToast(AddressEditActivity.this, "添加地址成功");
				ActivityDispatch.backToAboveActivity(ActivityDispatch
						.getActivityGroup(groupTag));
				break;
			}
			super.handleMessage(msg);
		}

	};

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.img_complete:
			getAddressInfo();
			if (StringUtils.isEmptyOrNull(address.getRecipient())
					|| StringUtils.isEmptyOrNull(address.getProvince())
					|| StringUtils.isEmptyOrNull(address.getCity())
					|| StringUtils.isEmptyOrNull(address.getArea())
					|| StringUtils.isEmptyOrNull(address.getStreet())
					|| StringUtils.isEmptyOrNull(address.getPostcode())
					|| StringUtils.isEmptyOrNull(address.getPhoneNum())) {
				ToastUtils.showToast(this, "地址信息不完整...");
				return;
			}

			if (!NetManager.isNetAvailable(this)) {// 无网络
				ToastUtils.showToast(this, "网络不可用...");
				return;
			}

			UICallbackInterface callbackInterface = new UICallbackInterface() {

				@Override
				public void onSuccess(CallbackData data) {
					handler.sendEmptyMessage(MSG_ADD_ADDRESS_SUCCESS);
				}

				@Override
				public void onError(String errorMsg) {
					handler.sendEmptyMessage(MSG_ADD_ADDRESS_FAIL);
				}
			};

			address.setAddressContent(address.getProvince() + "-"
					+ address.getCity() + "-" + address.getArea() + "-"
					+ address.getStreet());
			if (address.getRecipientId() != null) {
				AddressLogic.updateAddress(this, address, callbackInterface);
			} else {
				AddressLogic.addAddress(address, callbackInterface);

			}
			break;
		}
	}

	@Override
	protected void initParam() {
		address = (Address) intent
				.getSerializableExtra(ActivityDispatch.ADDRESS_KEY);
	}

	@Override
	protected void initView() {
		setContentView(R.layout.activity_addressedit);
		complete = (TextView) findViewById(R.id.img_complete);
		complete.setOnClickListener(this);

		addressName = (EditText) findViewById(R.id.edt_name); // 收件人姓名
		addressProvince = (EditText) findViewById(R.id.edt_province);// 收件人省份
		addressCity = (EditText) findViewById(R.id.edt_city); // 收件人城市
		addressArea = (EditText) findViewById(R.id.edt_zone); // 收件人地区
		addressStreet = (EditText) findViewById(R.id.edt_street); // 收件人街道
		addressPostcode = (EditText) findViewById(R.id.edt_postcode);// 收件人邮编
		addressPhone = (EditText) findViewById(R.id.edt_mobile); // 收件人手机
		addressTel = (EditText) findViewById(R.id.edt_tel);// 收件人电话
	}

	@Override
	protected void onResume() {
		super.onResume();
		showAddress();
	}

	private void showAddress() {
		if (address == null) {
			return;
		}

		addressName.setText(address.getRecipient());

		String addressContent = address.getAddressContent();
		String[] addressItem = addressContent.split("-");

		addressProvince.setText(addressItem[0]);
		addressCity.setText(addressItem[1]);
		addressArea.setText(addressItem[2]);
		addressStreet.setText(addressItem[3]);
		addressPostcode.setText(address.getPostcode());
		addressPhone.setText(address.getPhoneNum());
		addressTel.setText(address.getHomeNum());
	}

	private void getAddressInfo() {
		if (address == null) {
			address = new Address();
		}

		address.setRecipient(addressName.getText().toString());

		address.setProvince(addressProvince.getText().toString());

		address.setCity(addressCity.getText().toString());

		address.setArea(addressArea.getText().toString());

		address.setStreet(addressStreet.getText().toString());

		address.setPostcode(addressPostcode.getText().toString());

		address.setPhoneNum(addressPhone.getText().toString());

		address.setHomeNum(addressTel.getText().toString());

	}
}
